<?php

namespace App\Services\UserCall;


use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;

use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;

use App\Models\User;

class UserCall
{
	protected $callPhone;
	
	
	public function __construct($callPhone)
	{
		$this->callPhone = $callPhone;
		$this->user = request()->user();
		
	}

	 public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 必填，您的 AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 必填，您的 AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/Dyplsapi
        $config->endpoint = "dyplsapi.aliyuncs.com";
        return new OpenApiClient($config);
    }

	public static function createApiInfo(){
        $params = new Params([
            // 接口名称
            "action" => "BindAxb",
            // 接口版本
            "version" => "2017-05-25",
            // 接口协议
            "protocol" => "HTTPS",
            // 接口 HTTP 方法
            "method" => "POST",
            "authType" => "AK",
            "style" => "RPC",
            // 接口 PATH
            "pathname" => "/",
            // 接口请求体内容格式
            "reqBodyType" => "json",
            // 接口响应体内容格式
            "bodyType" => "json"
        ]);
        return $params;
    }
	
	
	public function call()
	{

		$client = self::createClient(env('ALIYUN_SMS_ACCESS_KEY_ID'),env('ALIYUN_SMS_ACCESS_KEY_SECRET'));
        $params = self::createApiInfo();

        $queries = [];
        $queries["PoolKey"] = "FC100000175590105";
        $queries["PhoneNoA"] = !empty($this->user->contact_phone) ? $this->user->contact_phone : $this->user->phone;

        $queries["PhoneNoB"] = $this->callPhone;
        $queries["Expiration"] = date("Y-m-d H:i:s",strtotime("+2 minutes",time()));
        // runtime options
        $runtime = new RuntimeOptions([]);
        $request = new OpenApiRequest([
            "query" => OpenApiUtilClient::query($queries)
        ]);
        // runtime options
     
        // 复制代码运行请自行打印 API 的返回值
        // 返回值为 Map 类型，可从 Map 中获得三类数据：响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。

        $result = $client->callApi($params, $request, $runtime);
        if($result['body']['Code'] != "OK"){

        	fail("当前用户忙，请稍后再拨");
        }
        $result = $result['body']; 
      
		return $result;
	}
}
